//
//  CommodityViewController.m
//  BabyAndBabyDriver
//
//  Created by 范琦 on 16/8/10.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "CommodityViewController.h"
#import "CommodityTopView.h"
#import "CommodityOneViewController.h"
#import "CommodityTwoViewController.h"
#import "CommodityThreeViewController.h"
#import "CommodityFourViewController.h"
#import "CommodityAddViewController.h"

@interface CommodityViewController ()

@property (nonatomic, strong) CommodityTopView *topView;

@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation CommodityViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _mainView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainView];
    [self creatTopView];
    [self creatScrollView];
    [self creatFourView];
    [self creatRight];
    
}

- (void)creatScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, KSCREENWIDTH, KSCREENHEIGHT)];
    _scrollView.contentSize = CGSizeMake(KSCREENWIDTH * 4, 0);
    _scrollView.pagingEnabled = YES;
    [self.mainView addSubview:_scrollView];
    
    [RACObserve(_scrollView, contentOffset) subscribeNext:^(id x) {
        
        NSValue *toValue = (NSValue *)x;
        CGPoint c;
        [toValue getValue:&c];
        
        NSInteger i = c.x / KSCREENWIDTH;
        CGRect rect = _topView.lineView.frame;

        _topView.lineView.frame = CGRectMake(i * _topView.oneBtn.bounds.size.width, rect.origin.y, rect.size.width, rect.size.height);
       
        
    }];
    
    
    
}
- (void)creatFourView
{
    CommodityOneViewController *oneVC = [[CommodityOneViewController alloc]init];
    [self addChildViewController:oneVC];
    oneVC.view.frame = CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height);
    [self.scrollView addSubview:oneVC.view];
    
    
    CommodityTwoViewController *twoVC = [[CommodityTwoViewController alloc]init];
    [self addChildViewController:twoVC];
    twoVC.view.frame = CGRectMake(KSCREENWIDTH, 0, KSCREENWIDTH,KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height);
    [self.scrollView addSubview:twoVC.view];


    CommodityThreeViewController *threeVC = [[CommodityThreeViewController alloc]init];
    [self addChildViewController:threeVC];
    threeVC.view.frame = CGRectMake(KSCREENWIDTH * 2, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height);
    [self.scrollView addSubview:threeVC.view];

    CommodityFourViewController *fourVC = [[CommodityFourViewController alloc]init];
    [self addChildViewController:fourVC];
    fourVC.view.frame = CGRectMake(KSCREENWIDTH * 3, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height);
    [self.scrollView addSubview:fourVC.view];

    

}
- (void)creatRight
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"添加" forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 30, 20);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];

    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        CommodityAddViewController *add = [[CommodityAddViewController alloc]init];
        [self.navigationController pushViewController:add animated:YES];
    }];

}

- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"CommodityTopView" owner:nil options:nil] lastObject];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 40);
    _topView.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
    [self.mainView addSubview:_topView];
    _topView.lineView.backgroundColor = BACKGROUNDCOLOR;
    
    [[_topView.twoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       
        CGRect rect = _topView.lineView.frame;
        _topView.lineView.frame = CGRectMake(_topView.twoBtn.frame.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        [_scrollView setContentOffset:CGPointMake(KSCREENWIDTH, 0) animated:YES];
        
    }];
    [[_topView.oneBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        CGRect rect = _topView.lineView.frame;
        _topView.lineView.frame = CGRectMake(_topView.oneBtn.frame.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

        
    }];
    
    [[_topView.threeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        CGRect rect = _topView.lineView.frame;
        _topView.lineView.frame = CGRectMake(_topView.threeBtn.frame.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        [_scrollView setContentOffset:CGPointMake(KSCREENWIDTH * 2, 0) animated:YES];

        
    }];
    
    [[_topView.fourBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        CGRect rect = _topView.lineView.frame;
        _topView.lineView.frame = CGRectMake(_topView.fourBtn.frame.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        [_scrollView setContentOffset:CGPointMake(KSCREENWIDTH * 3, 0) animated:YES];

        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
